#include "device.h"
#include "client.h"

Device::Device(QObject *parent): QObject{parent}
{
    device_id(0);
    task_type(Types::NONE);
    cur_current(Types::AMPL_1A);
    // last_update_time(QDateTime::currentDateTime());
    cur_voltage(0.0f);
    // Initialize empty lists for results and tasks
    results(QList<Result *>());
    tasks(QList<Task *>());
}

Device::Device(QObject *parent, quint8 id, Types::DeviceStatus status)
    : QObject{parent}
{
    device_id(id);
    device_status(status);
    // last_update_time(QDateTime::currentDateTime());
    task_type(Types::NONE);
    cur_current(Types::AMPL_1A);
    cur_voltage(0.0f);
    // Initialize empty lists for results and tasks
    results(QList<Result *>());
    tasks(QList<Task *>());
}

void Device::measure(Types::TaskType type, Types::DrivingCurrent current, quint16 frequency)
{
    // 开始测量 0x41+1byte(设备编号)+1byte(测量模式)+1byte(采样电流档位)+1byte(单点采样频率)
    QByteArray data;
    data.append(static_cast<char>(0x41));
    data.append(static_cast<char>(device_id()));
    data.append(static_cast<char>(type));
    data.append(static_cast<char>(current));
    // 小端
    data.append(static_cast<char>(frequency & 0xFF));
    data.append(static_cast<char>((frequency >> 8) & 0xFF));
    qobject_cast<Client *>(parent())->socket->write(data);
}
